local liehe = fk.CreateSkill{
  name = "xiaobai__liehe",
}

Fk:loadTranslationTable{
  ["xiaobai__liehe"] = "列合",
  [":xiaobai__liehe"] = "你失去过牌的回合结束时，可令你或当前回合角色执行恰好能使装备区牌数等于对方的一项：1.可以使用一张装备牌；2.收回或移动自己场上一张装备牌。",
  ["liehe_use"] = "令 %dest 使用一张其空置装备栏对应副类别的装备牌",
  ["liehe_use_unlimited"] = "令 %dest 使用一张装备牌",
  ["liehe_replace"] = "令 %dest 使用一张装备牌以替换装备区里已有的装备牌",
  ["liehe_get"] = "令 %dest 获得自己装备区一张装备牌",
  ["liehe_move_other"] = "令 %dest 将一张装备牌移动至其他角色的装备区",
  ["#liehe_use"] = "你可以发动列合：选择一项",
  ["#liehe_askUse"] = "你可以使用一张对应副类别的装备牌",
  ["liehe_choose2Move"] = "你选择一其他名角色然后需将一张装备牌移至其角色的装备区",
  ["liehe_move_to"] = "列合：你可以令 %dest 将装备区的的一张牌移动至 %src 的对应区域",  
  ["#liehe_getEquip"] = "列合：你须获得自己的一张装备牌",
  ["liehe_get_or_move_other"] = "令 %dest 获得自己一张装备装备区的牌或将装备区牌移给其他角色", 
  ["liehe_choose"] = "列合：选择一项"
}

liehe:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    local evt = player.room.logic:getEventsOfScope(
      GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == player then
            return true
          end
        end
      end,Player.HistoryTurn
    )
    return evt and #evt>0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to  = room.current
    local difference = #player:getCardIds("e") - #to:getCardIds("e")
    local types = {"weapon", "armor", "defensive_ride", "offensive_ride", "treasure"}
    local subtypes = {Card.SubtypeWeapon, Card.SubtypeArmor, Card.SubtypeDefensiveRide,Card.SubtypeOffensiveRide, Card.SubtypeTreasure}
    local cost_data = {}
    local ret = false
    local moveTargets = table.filter(room:getOtherPlayers(player),function (element, index, array)
      return element ~= to and player:canMoveCardsInBoardTo(element,"e")
    end)
    if difference == 1 then
      local all_choices = {"liehe_use::"..to.id, "liehe_get::"..player.id, "liehe_move_other::"..player.id, "Cancel"}
      local can_choose = {true, #player:getCardIds("e")>0, #moveTargets>0, true}
      local choices = table.filter(all_choices, function (element, index, array)
        return can_choose[index]
      end)
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = liehe.name,
        prompt = "#liehe_use",
        all_choices = all_choices})
      if choice == "Cancel" then
        return false
      elseif choice:startsWith("liehe_use") then
        local type = table.concat(table.map(
          table.filter(subtypes, 
            function (type_name) return not player:getEquipment(type_name) end),
          function (element, index, array)return types[index] end), ',')
        local card = room:askToUseRealCard(to, {
          pattern = table.filter(to:getHandlyIds(true), function(id) return Fk:getCardById(id).type == Card.TypeEquip end),
          skill_name = liehe.name,
          prompt = "#liehe_askUse",
          cancelable = true})
        if card then
          cost_data.card = card
          cost_data.operation = 1
          ret = true
        end
      elseif choice:startsWith("liehe_get") then
        local card = room:askToCards(player, {
          max_num = 1,
          min_num = 1,
          include_equip = true,
          skill_name = liehe.name,
          cancelable = false,
          pattern = ".|.|.|equip",
          prompt = "#liehe_getEquip"})
        if card then
          cost_data.card = card
          cost_data.executor = player
          cost_data.operation = 2
          ret = true
        end
      else
        local moveTo = room:askToChoosePlayers(player, {
          targets = moveTargets,
          min_num = 1,
          max_num = 1,
          prompt = "liehe_choose2Move",
          skill_name = liehe.name,
          cancelable = false})
        if moveTo and #moveTo>0 then
          cost_data.target = moveTo[1]
          cost_data.executor = player
          cost_data.operation = 3
          ret = true
        end
      end
      ret = true
    elseif difference == -1 then
      local moveTargets = table.filter(room:getOtherPlayers(to),function (element, index, array)
        return element ~= player and to:canMoveCardsInBoardTo(element, "e")
      end)
      local all_choices = {"liehe_use::"..player.id, "liehe_get_or_move_other::"..to.id, "Cancel"}
      local can_choose = {true,#to:getCardIds("e")>0 or #moveTargets>0,true}
      local choices = table.filter(all_choices,function (element, index, array)
        return can_choose[index]
      end)
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = liehe.name,
        prompt = "#liehe_use",
        all_choices = all_choices})
      if choice == "Cancel" then
        return false
      elseif choice:startsWith("liehe_use") then
        local type = table.concat(table.map(
          table.filter(subtypes, 
            function (type_name) return not to:getEquipment(type_name) end),
          function (element, index, array)return types[index] end),',')
        local pattern = ".|.|.|.|.|"..type
        local card = room:askToUseRealCard(player,{
          pattern = table.filter(player:getHandlyIds(true), function(id) return Exppattern:Parse(pattern):match(Fk:getCardById(id)) end),
          skill_name = liehe.name,
          prompt = "#liehe_askUse"})
        if card then
          cost_data.card = card
          cost_data.operation = 1
          ret = true
        end
      elseif choice:startsWith("liehe_get") then
        local all_choices1 = {"liehe_get::"..to.id,"liehe_move_other::"..to.id}
        local choices1 = {}
        if not #to:getCardIds("e") then
          choices1 = {all_choices1[2]}
        elseif not #moveTargets then
          choices1 = {all_choices1[1]}
        else
          choices1 = all_choices1
        end
        choice = room:askToChoice(to, {
          choices = choices1,
          skill_name = liehe.name,
          prompt =  "liehe_choose",
          all_choices = all_choices1})
        if choice:startsWith("liehe_get") then
            local card = room:askToChooseCard(player, {
              target = to,
              flag = "e",
              min_num = 1,
              max_num = 1,
              skill_name = liehe.name,
              pattern = ".|.|.|equip",
              prompt = "#liehe_getEquip"})
          if card then
            cost_data.card = card
            cost_data.executor = to
            cost_data.operation = 2
            ret = true
          end
        else
          local moveTo = room:askToChoosePlayers(to, {
            targets = moveTargets,
            min_num = 1,
            max_num = 1,
            prompt = "liehe_choose2Move",
            skill_name = liehe.name,
            cancelable = false})
          if moveTo and #moveTo > 0  then
            cost_data.target = moveTo[1]
            cost_data.executor = to
            cost_data.operation = 3
            ret = true
          end
        end
      end
      ret = true
    elseif difference == 2 and player:canMoveCardsInBoardTo(to,"e") then
      if room:askToSkillInvoke(player, {skill_name = liehe.name, prompt = "liehe_move_to:"..to.id..":"..player.id}) then
        cost_data = {executor = player,operation = 3,target = to}
        ret = true
      end
    elseif difference == -2 and to:canMoveCardsInBoardTo(player,"e") then
      if room:askToSkillInvoke(player, {skill_name = liehe.name, prompt = "liehe_move_to:"..player.id ..":"..to.id}) then
        cost_data = {executor = to,operation = 3,target = player}
        ret = true
      end
    elseif target == player then
      local moveTargets = table.filter(room:getOtherPlayers(player),function (element, index, array)
        return to:canMoveCardsInBoardTo(element,"e")
      end)
      local all_choices = {"liehe_use_unlimited::"..player.id,"liehe_get::"..player.id,"liehe_move_other::"..player.id,"Cancel"}
      local can_choose = {true,#player:getCardIds("e")>0,#moveTargets>0,true}
      local choices = table.filter(all_choices,function (element, index, array)
        return can_choose[index]
      end)
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = liehe.name,
        prompt = "#liehe_use",
        all_choices = all_choices})
      if choice == "Cancel" then
        return false
      elseif choice:startsWith("liehe_use") then
        local card = room:askToUseRealCard(player, {
          pattern = table.filter(player:getHandlyIds(true), function(id) return Fk:getCardById(id).type == Card.TypeEquip end),
          skill_name = liehe.name,
          prompt = "#liehe_askUse"})
        if card then
          cost_data.card = card
          cost_data.operation = 1
          ret = true
        end
      elseif choice:startsWith("liehe_get") then
        local card = room:askToCards(player, {
          max_num = 1,
          min_num = 1,
          include_equip = true,
          skill_name = liehe.name,
          pattern = ".|.|.|equip",
          prompt = "#liehe_getEquip"})
        if card then
          cost_data.card = card
          cost_data.executor = player
          cost_data.operation = 2
          ret = true
        end
      else
        local moveTo = room:askToChoosePlayers(player, {
          targets = moveTargets,
          min_num = 1,
          max_num = 1,
          prompt = "liehe_choose2Move",
          skill_name = liehe.name,
          cancelable = false})
        if moveTo and #moveTo > 0 then
          cost_data.target = moveTo[1]
          cost_data.executor = player
          cost_data.operation = 3
          ret = true
        end
      end
    elseif difference == 0 and #player:getCardIds("e")>0 then
      local choices = {"liehe_replace::"..player.id,"liehe_replace::"..to.id,"Cancel"}
      local choice = room:askToChoice(player,{
        choices = choices,
        skill_name = liehe.name})
      if choice == "Cancel" then return end
      if choice == choices[1] then
        to = player
      end
      local type = table.concat(table.map(
          table.filter(subtypes, 
            function (type_name) return to:getEquipment(type_name) ~= nil end),
          function (element, index, array)return types[index]end),',')
      local pattern = ".|.|.|.|.|"..type
      local card = room:askToUseRealCard(to, {
        pattern = table.filter(player:getHandlyIds(true), function(id) return Exppattern:Parse(pattern):match(Fk:getCardById(id)) end),
        skill_name = liehe.name,
        prompt = "#liehe_askUse"})
      if card then
        cost_data.card = card
        cost_data.operation = 1
        ret = true
      end
    end
    if ret then
      event:setCostData(self, table.simpleClone(cost_data))
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.operation == 1 then
      room:useCard(cost_data.card)
    elseif cost_data.operation == 2 then
      room:obtainCard(cost_data.executor,cost_data.card,false,fk.ReasonPrey,player,liehe.name)
    elseif cost_data.operation == 3 then
      room:askToMoveCardInBoard(cost_data.executor, {
        target_one = cost_data.executor,
        target_two = cost_data.target,
        skill_name = liehe.name,
        flag = "e",
        move_from = cost_data.executor})
    end
  end
})

return liehe